<template>
  <!-- 原材料需求计划表单弹窗 -->
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="1100px"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="160">
      <el-row>
        <el-col :span="12">
          <el-form-item label="工厂编号" prop="plantCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.plantCode"
              placeholder="请输入工厂编号"
              clearable
            />

            <span v-else>{{ formData.plantCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="工作中心名称" prop="workCenterName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workCenterName"
              placeholder="请输入工作中心名称"
              clearable
            />

            <span v-else>{{ formData.workCenterName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="生产计划编号" prop="producePlanCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.producePlanCode"
              placeholder="请输入生产计划编号"
              clearable
            />

            <span v-else>{{ formData.producePlanCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划编号" prop="workOrderCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.workOrderCode"
              placeholder="请输入计划编号"
              clearable
            />

            <span v-else>{{ formData.workOrderCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划年份" prop="planYear">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planYear"
              placeholder="请输入计划年份"
              clearable
            />

            <span v-else>{{ formData.planYear }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="计划周数" prop="planWeek">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.planWeek"
              placeholder="请输入计划周数"
              clearable
            />

            <span v-else>{{ formData.planWeek }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料编号" prop="materialCode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialCode"
              placeholder="请输入物料编号"
              clearable
            />

            <span v-else>{{ formData.materialCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料条码" prop="materialBarcode">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialBarcode"
              placeholder="请输入物料条码"
              clearable
            />

            <span v-else>{{ formData.materialBarcode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="物料名称" prop="materialName">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.materialName"
              placeholder="请输入物料名称"
              clearable
            />

            <span v-else>{{ formData.materialName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否已执行" prop="isExecute">
            <el-select
              v-if="!props.isReadonly"
              v-model="formData.isExecute"
              placeholder="请选择是否已执行"
              clearable
            >
              <el-option
                v-for="item in dictionaryStore.filterDictList('YesNo')"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>

            <span v-else>{{
              dictionaryStore.filterDictName("YesNo", formData.isExecute)
            }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求数量" prop="requestQuantity">
            <el-input
              v-if="!props.isReadonly"
              v-model="formData.requestQuantity"
              placeholder="请输入需求数量"
              clearable
            />

            <span v-else>{{ formData.requestQuantity }}</span>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "MESMESMATERIALREQUESTPLANADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  plantCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workCenterName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  producePlanCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  workOrderCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  planYear: [],
  planWeek: [],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialBarcode: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  isExecute: [],
  requestQuantity: [],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  organizationId: "",
  organizationCode: "",
  plantId: "",
  plantCode: "",
  workCenterId: "",
  workCenterCode: "",
  workCenterName: "",
  producePlanId: "",
  producePlanCode: "",
  workOrderId: "",
  workOrderCode: "",
  planDate: "",
  planYear: "",
  planWeek: "",
  shiftSystemId: "",
  shiftId: "",
  materialId: "",
  materialCode: "",
  materialBarcode: "",
  materialName: "",
  sequenceCode: "",
  isRelease: "",
  isExecute: "",
  yield: "",
  actualYield: "",
  requestQuantity: "",
  finishQuantity: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  deleted: "",
  theoryQuantity: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      const {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        workCenterName,
        producePlanId,
        producePlanCode,
        workOrderId,
        workOrderCode,
        planDate,
        planYear,
        planWeek,
        shiftSystemId,
        shiftId,
        materialId,
        materialCode,
        materialBarcode,
        materialName,
        sequenceCode,
        isRelease,
        isExecute,
        actualYield,
        requestQuantity,
        finishQuantity,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
        theoryQuantity,
      } = newVal;
      Object.assign(formData, {
        id,
        organizationId,
        organizationCode,
        plantId,
        plantCode,
        workCenterId,
        workCenterCode,
        workCenterName,
        producePlanId,
        producePlanCode,
        workOrderId,
        workOrderCode,
        planDate,
        planYear,
        planWeek,
        shiftSystemId,
        shiftId,
        materialId,
        materialCode,
        materialBarcode,
        materialName,
        sequenceCode,
        isRelease,
        isExecute,
        actualYield,
        requestQuantity,
        finishQuantity,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        deleted,
        theoryQuantity,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.organizationId = "";
  formData.organizationCode = "";
  formData.plantId = "";
  formData.plantCode = "";
  formData.workCenterId = "";
  formData.workCenterCode = "";
  formData.workCenterName = "";
  formData.producePlanId = "";
  formData.producePlanCode = "";
  formData.workOrderId = "";
  formData.workOrderCode = "";
  formData.planDate = "";
  formData.planYear = "";
  formData.planWeek = "";
  formData.shiftSystemId = "";
  formData.shiftId = "";
  formData.materialId = "";
  formData.materialCode = "";
  formData.materialBarcode = "";
  formData.materialName = "";
  formData.sequenceCode = "";
  formData.isRelease = "";
  formData.isExecute = "";
  formData.yield = "";
  formData.actualYield = "";
  formData.requestQuantity = "";
  formData.finishQuantity = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.deleted = "";
  formData.theoryQuantity = "";
}

const loading = ref(false);

/** 原材料需求计划保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/mes/mesmaterialrequestplan/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
